package com.freedo.demo.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.freedo.demo.entity.User;
import com.freedo.demo.service.i.IUserService;

@RestController
public class UserController {

	@Resource
	private IUserService userService;
	
	@RequestMapping(value = "/user/get/{id}", method = { RequestMethod.GET, RequestMethod.POST })
	public User getUser(@PathVariable String id) {
		return userService.getUser(id);
	}
	
	@RequestMapping(value = "/user/count", method = { RequestMethod.GET, RequestMethod.POST })
	public int getUserCount() {
		return userService.getUserCount();
	}

	@RequestMapping(value = "/user/all", method = { RequestMethod.GET, RequestMethod.POST })
	public List<User> getUsers() {
		return userService.getUsers();
	}
	
	@RequestMapping(value = "/user/page", method = { RequestMethod.GET, RequestMethod.POST })
	public List<User> getUsersPage(@RequestBody Map<String,Object> params) {
		return userService.getUsersPage((int)params.get("offset"), (int)params.get("pagecount"));
	}


	@RequestMapping(value = "/user/create", method = RequestMethod.POST)
	public User createUser(@RequestBody(required = false) User u) {
		if (u == null) {
			u = new User();
		}
		userService.insertUser(u);
		return u;
	}

}
